/*!
 * Ext JS Library 3.3.1
 * Copyright(c) 2006-2010 Sencha Inc.
 * licensing@sencha.com
 * http://www.sencha.com/license
 */
Ext.app.App = function(cfg) {
    Ext.apply(this, cfg);
    this.addEvents({
        'ready' : true,
        'beforeunload' : true
    });

    Ext.onReady(this.initApp, this);
};

Ext.extend(Ext.app.App, Ext.util.Observable, {
    isReady: false,
    startMenu: null,
    modules: null,

    paracol: Paracol(DefaultParacolBasicMessageFilters.addFieldsAsObject,
            ParacolExtJSMessageFilters({
                createComponent: this.componentBuilder
            }),
            DefaultParacolBasicMessageFilters.handleJsonDate),

    metaParacol: Paracol(DefaultParacolBasicMessageFilters.addFieldsAsObject,
            ParacolExtJSMessageFilters({
                createComponent: this.componentBuilder
            }),
            DefaultParacolBasicMessageFilters.handleJsonDate),

    getStartConfig : function() {

    },

    showWaitBox: function(message) {
        Ext.MessageBox.show({
            msg: message,
            progressText: 'Wait ...',
            width:300,
            wait:true,
            waitConfig: {interval:200},
            icon:'ext-mb-download', //custom class in msg-box.html
            animEl: 'mb7'
        });
        Ext.MessageBox.getDialog().toFront();
    },

    hideWaitBox: function() {
        Ext.MessageBox.hide();
    },

    initApp : function() {
        this.startConfig = this.startConfig || this.getStartConfig();

        this.desktop = new Ext.Desktop(this);

        this.launcher = this.desktop.taskbar.startMenu;

        this.modules = this.getModules();

        if (this.modules) {
            this.initModules(this.modules);
        }

//        var this_ = this;
//        this.httpListener = {
//            onRequestSent: function(message) {
//                this_.showWaitBox();
//            },
//
//            onResponseReceived: function(message) {
//                this_.hideWaitBox();
//            }
//        };

        this.init();

        this.paracol.setErrorHandler(function(errorMessage) {
            alert(errorMessage.message + "(status:" + errorMessage.status + ")");
        });
        //this.paracol.addHttpListener(this.httpListener);
        this.metaParacol.dispatcherUrl = "dispMeta/";
        this.metaParacol.setErrorHandler(function(errorMessage) {
            alert(errorMessage.message + "(status:" + errorMessage.status + ")");
        });
        //this.metaParacol.addHttpListener(this.httpListener);

        Ext.EventManager.on(window, 'beforeunload', this.onUnload, this);

        this.fireEvent('ready', this);
        this.isReady = true;
    },

    getModules : Ext.emptyFn,
    init : Ext.emptyFn,

    initModules : function(ms) {
        for (var i = 0, len = ms.length; i < len; i++) {
            var m = ms[i];
            if (m.initModule) m.initModule(this);
            this.launcher.add(m.launcher);
            m.app = this;
        }
    },

    getModule : function(name) {
        var ms = this.modules;
        for (var i = 0, len = ms.length; i < len; i++) {
            if (ms[i].id == name || ms[i].appType == name) {
                return ms[i];
            }
        }
        return '';
    },

    onReady : function(fn, scope) {
        if (!this.isReady) {
            this.on('ready', fn, scope);
        } else {
            fn.call(scope, this);
        }
    },

    getDesktop : function() {
        return this.desktop;
    },

    onUnload : function(e) {
        if (this.fireEvent('beforeunload', this) === false) {
            e.stopEvent();
        }
    }
});